package tech.aistar.controller.authordis;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import tech.aistar.entity.authordis.Notes;
import tech.aistar.service.authordis.INoteTravelerDisService;
import tech.aistar.vo.travelerlist.NoteAndPersonVo;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.zip.Inflater;


/**
 * Created by lenovo on 2019/10/1.
 */

@Controller
@RequestMapping("/travelernoteList")
public class TravelerNotesList {

    @Autowired
    private INoteTravelerDisService noteTravelerDisService;

    @GetMapping("/authordis")
    public String notes(){
        return "authordis/travelernotes";
    }

    /**
     * 根据游记的浏览量和评论数进行热度展示
     * 异步分页展示
     * @param model
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/travelernotes")
    public String hot(Model model,@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "8") Integer pageSize){
        //调用service进行分页数据的获取
        Map<String,Object> map = noteTravelerDisService.findNoteByHot(pageNum,pageSize);

        PageInfo<Notes> pageInfo = (PageInfo<Notes>) map.get("pageInfo");
        List<NoteAndPersonVo> noteAndPersonVos = (List<NoteAndPersonVo>) map.get("noteAndPersonVos");
        Collections.sort(noteAndPersonVos, new Comparator<NoteAndPersonVo>(){
            /*
             * int compare(Person p1, Person p2) 返回一个基本类型的整型，
             * 返回负数表示：p1 小于p2，
             * 返回0 表示：p1和p2相等，
             * 返回正数表示：p1大于p2
             */
            public int compare(NoteAndPersonVo n1, NoteAndPersonVo n2) {
                Double a = n1.getNotes().getSupport()*0.4+n1.getNotes().getCommentsum()*0.6;
                Double b = n2.getNotes().getSupport()*0.4+n2.getNotes().getCommentsum()*0.6;
                //按照Person的年龄进行升序排列
                if(a > b){
                    return 1;
                }
                if(a == b){
                    return 0;
                }
                return -1;
            }
        });

        //将获取的数据保存到作用域里
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("noteAndPersonVos",noteAndPersonVos);
//        System.out.println(pageInfo);
        model.addAttribute("t1",1);
        model.addAttribute("t2",0);
        //返回数据到页面
        return "authordis/travelernotes";
    }

    @RequestMapping("/travelernotestime")
    public String time(Model model,String time,@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "8") Integer pageSize){

        //设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        //获取系统当前时间
        Date now = new Date();
        //对时间域单独修改 -
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        //一年之内
        calendar.add(Calendar.YEAR,-1);
        Date date = calendar.getTime();
        time = df.format(date);
//        System.err.println("//////////////////////////"+time);
        //调用service进行分页数据的获取
        Map<String,Object> map = noteTravelerDisService.findNoteByTime(time,pageNum,pageSize);

        PageInfo<Notes> pageInfo = (PageInfo<Notes>) map.get("pageInfo");
        List<NoteAndPersonVo> noteAndPersonVos = (List<NoteAndPersonVo>) map.get("noteAndPersonVos");
        //将获取的数据保存到作用域里
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("noteAndPersonVos",noteAndPersonVos);
        System.out.println(pageInfo);
        model.addAttribute("t1",0);
        model.addAttribute("t2",1);
        //返回数据到页面
        return "authordis/travelernotes";
    }




}
